//
//  Button.m
//  Return Of The Lemi
//
//  Created by Paul Vishayanuroj on 11/10/09.
//  Copyright 2009 Cornell University. All rights reserved.
//

#import "Button.h"


@implementation Button

+ (id) ButtonWithImage:(NSString *)file file2:(NSString *)file2 position:(CGPoint)pos target:(id)target sel1:(SEL)sel1 sel2:(SEL)sel2
{
	Menu *menu = [Menu menuWithItems:[ButtonItem ButtonItemWithImage:file file2:file2 target:target sel1:sel1 sel2:sel2], nil];
	menu.position = pos;
	menu.isTouchEnabled = YES;

	return menu;
}

@end


@implementation ButtonItem

@synthesize desiredPos, buttonMoveState;

+ (id) ButtonItemWithImage:(NSString *)file file2:(NSString *)file2 target:(id)target sel1:(SEL)sel1 sel2:(SEL)sel2
{
	return [[[self alloc] InitWithImage:file file2:file2 target:target sel1:sel1 sel2:sel2] autorelease];
}

// Instance method constructor
- (id) InitWithImage:(NSString *)file file2:(NSString *)file2 target:(id)target sel1:(SEL)sel1 sel2:(SEL)sel2
{
	if ((self = [super initWithTarget:target selector:nil]))
	{
		unpressed = [[Sprite spriteWithFile: file] retain];
		pressed = [[Sprite spriteWithFile: file2] retain];		
		
		press = sel1;
		depress = sel2;
		tar = [target retain];
		
		// This is important to make the buttonItem the right size
		self.contentSize = unpressed.contentSize;
		
		// Coordinates of the anchor point - (0,0) is the bottom-left
		unpressed.anchorPoint = ccp(0,0);
		pressed.anchorPoint = ccp(0,0);
		
		buttonMoveState = kLocked;
		
		// Add the unpressed sprite to the button object
		[self addChild: unpressed];
	}
	return self;
}

- (void) Tick
{
	switch(buttonMoveState)
	{
		case kMovingLeft:
			// See if we have reached the desired position 
			if (self.position.x <= desiredPos.x)
			{
				self.position = desiredPos;
				buttonMoveState = kLocked;
			}
			// Otherwise keep moving
			else
			{
				self.position = CGPointMake(self.position.x - BUTTON_MOVE_SPEED, self.position.y);
			}
			break;
		case kMovingRight:
			// See if we have reached the desired position 
			if (self.position.x >= desiredPos.x)
			{
				self.position = desiredPos;
				buttonMoveState = kLocked;
			}
			// Otherwise keep moving
			else
			{
				self.position = CGPointMake(self.position.x + BUTTON_MOVE_SPEED, self.position.y);
			}			
			break;
		case kLocked:
		default:
			break;
	}
}

// Override method from Menu Item class
-(void) selected 
{
	//NSLog(@"selected");
	
	[tar performSelector:press];
	[self removeChild:unpressed cleanup:NO];
	[self addChild:pressed];
	[super selected];
}

// Override method from Menu Item class
-(void) unselected 
{
	//NSLog(@"unselected");
	
	[tar performSelector: depress];
	[self removeChild:pressed cleanup:NO];
	[self addChild:unpressed];
	[super unselected];
}

// Override method from Menu Item class
- (void) activate 
{
	// If used with the dock, this should probably somehow check that the button has not moved
	//NSLog(@"activated: do something");
}

- (void) dealloc
{
	[pressed release];
	[unpressed release];
	[tar release];
	[super dealloc];
}

@end
